package com.jtlrm.ckd.mvp.view.fragment;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.CommonUtil;
import com.base.sdk.widget.TitleBar;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.fragment.BaseFragment;
import com.jtlrm.ckd.entity.HuanZheInfoEntity;
import com.jtlrm.ckd.entity.PageEntity;
import com.jtlrm.ckd.mvp.model.HuanZheGuanLiModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.mvp.view.activity.HuanZheManagerActivity;
import com.jtlrm.ckd.mvp.view.activity.NewHuanZheJianDangActivity;
import com.jtlrm.ckd.mvp.view.adapter.HuanZheGuanLiAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import butterknife.BindView;

/**
 * 患者管理
 */
public class HuanZheGuanLiFragment extends BaseFragment {
    @BindView(R.id.huanzhe_guanli_title)
    TitleBar titleBar;
    @BindView(R.id.huanzhe_list_result)
    TextView huanzheRenShu;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.search_content)
    EditText searchContent;
    private int pageNum = 1;
    private int pageSize = 12;
    HuanZheGuanLiAdapter adapter;
    HuanZheGuanLiModel huanZheGuanLiModel;
    String keyWords = "";
    @BindView(R.id.clear_search)
    ImageView clearSearchI;
    @Override
    protected int setContentLayout() {
        return R.layout.fragment_huan_zhe_guan_li;
    }

    @Override
    protected void initView() {
        titleBar.tvMiddle.setText("患者管理");
        if (UserHelper.getInstance(context).isDoctor()) {
            titleBar.tvRight.setText("新患者建档");
            titleBar.tvRight.setVisibility(View.VISIBLE);
            titleBar.tvRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(context, NewHuanZheJianDangActivity.class));
                }
            });
        }
        adapter = new HuanZheGuanLiAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(createItemDecoration());
        huanZheGuanLiModel = new HuanZheGuanLiModel();
    }

    @Override
    protected boolean isImmersionBarEnabled() {
        return true;
    }


    @Override
    protected void obtainData() {
        adapter.openLoadAnimation();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNum++;
                loadData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                loadData();
            }
        });
        smartRefreshLayout.autoRefresh();
    }

    private void loadData() {
        huanZheGuanLiModel.queryHuanZheList(pageNum, pageSize, keyWords, new CommonObserver<PageEntity<List<HuanZheInfoEntity>>>() {
            @Override
            public void onError(int errType, String errMessage) {
                closeFrshorLoadmore();
                showToast(errMessage);
            }

            @Override
            public void onResult(PageEntity<List<HuanZheInfoEntity>> data) {
                if (pageNum > 1) {
                    adapter.addData(data.getItems());
                } else {
                    adapter.replaceData(data.getItems());
                }
                if (data.getItems().size() < pageSize) {
                    smartRefreshLayout.setEnableLoadmore(false);
                } else {
                    smartRefreshLayout.setEnableLoadmore(true);
                }
                setText(huanzheRenShu, "共" + data.getRecordCount() + "人");
                closeFrshorLoadmore();
            }
        }, lifecycleSubject);
    }

    @Override
    protected void initEvent() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HuanZheInfoEntity entity = (HuanZheInfoEntity) adapter.getData().get(position);
                HuanZheManagerActivity.goActivity(context, entity.getId());
            }
        });
        searchContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    pageNum = 1;
                    CommonUtil.hideSoftInput(context, searchContent);
                    smartRefreshLayout.autoRefresh();
                }
                return false;
            }
        });
        searchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                keyWords = s.toString();
            }
        });
        clearSearchI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setText(searchContent, "");
                smartRefreshLayout.autoRefresh();
            }
        });
    }


    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(getActivity());
        mImmersionBar.keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarWithKitkatEnable(false).init();

    }
}
